<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="批次编号" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入批次编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="溯源码" prop="tracingCode">
        <el-input
          v-model="queryParams.tracingCode"
          placeholder="请输入溯源码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="stratTime">
        <el-date-picker clearable
                        v-model="queryParams.stratTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期" prop="days">
        <el-input
          v-model="queryParams.days"
          placeholder="请输入有效期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农户名称" prop="farmerName">
        <el-input
          v-model="queryParams.farmerName"
          placeholder="请输入农户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:products:remove']"
        >批量还原</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编号" align="center" prop="productId" />
      <el-table-column label="产物名称" align="center" prop="goodsName" />
      <el-table-column label="批次编号" align="center" prop="materialCode" />
      <el-table-column label="溯源码" align="center" prop="tracingCode" />
      <el-table-column label="创建时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="days" />
      <el-table-column label="农户名称" align="center" prop="farmerName" />
      <el-table-column label="地址" align="center" prop="address"  width="180"/>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status==='0'" size="small" type="success">未出/入库</el-tag>
          <el-tag v-if="scope.row.status==='1'" size="small" type="success">已出库</el-tag>
          <el-tag v-if="scope.row.status==='2'" size="small" type="success">已入库</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:products:remove']"
          >还原</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTrueDelete(scope.row)"
            v-hasPermi="['product:products:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {delFlagProduct, ProductList,delProductById} from "@/api/product/products";


export default {
  name: "index",
  data() {
    return {
      // 遮罩层
      productList: [],
      options: [],
      cityProps:{
        value:"id",
        label:"name",
      },
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialCode: null,
        tracingCode: null,
        stratTime: null,
        endTime: null,
        days: null,
        farmerName: null,
        xid: null,
        yid: null,
        zid: null,
        status: null,
      },
      // 表单参数
      form: {gids:[]},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      ProductList(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 还原按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal.confirm('是否确认还原产品编号为"' + productIds + '"的数据项？').then(function() {
        return delFlagProduct(productIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("还原成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleTrueDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal.confirm('是否确认还原产品编号为"' + productIds + '"的数据项？').then(function() {
        return delProductById(productIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("还原成功");
      }).catch(() => {});
    },
  }
};
</script>
